﻿using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ShapeManager;
using System.Drawing;
using Commands;
using Tools;
using ROI编辑器.Tools;

namespace UserControls
{
    public partial class UImageWindow : Control
    {
        #region 字段 | 属性
        private Form owner;                 //所有者
        private Tool[] tools;               //工具数组
        private ShapeList shapeList;        //形状列表
        private UndoManager undoManager;    //撤销管理
        private ShapeType activeTool;       //当前类型

        /// <summary> 所有者 </summary>
        public Form Owner { get => owner; set => owner = value; }
        /// <summary> 形状列表 </summary>
        public ShapeList ShapeList { get => shapeList; set => shapeList = value; }
        /// <summary> 撤销管理 </summary>
        public UndoManager UndoManager { get => undoManager; set => undoManager = value; }
        /// <summary> 激活的工具类型 </summary>
        public ShapeType ActiveTool { get => activeTool; set => activeTool = value; }
        /// <summary> 是否可以重做 </summary>
        public bool CanUndo
        {
            get
            {
                if (undoManager != null)
                    return undoManager.CanUndo();
                return false;
            }
        }
        /// <summary> 是否可以撤销 </summary>
        public bool CanRedo
        {
            get
            {
                if (undoManager != null)
                    return undoManager.CanRedo();
                return false;
            }
        }
        #endregion

        #region 构造函数、初始化
        public UImageWindow()
        {
            this.BackColor = Color.LightGray;
            this.DoubleBuffered = true;
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
            UpdateStyles();
            ShapeList = new ShapeList();
            UndoManager = new UndoManager(shapeList);
        }
        /// <summary> 初始化 </summary>
        public void Initialize(Form owner)
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
            this.Owner = owner;
            // 创建形状列表
            ShapeList = new ShapeList();
            // 创建重做管理
            UndoManager = new UndoManager(shapeList);
            // 创建绘制工具数组
            tools = new Tool[(int)ShapeType.NumberOfDrawTools];
            tools[(int)ShapeType.Pointer] = new ToolPointer();
            tools[(int)ShapeType.Rectangle] = new ToolRectangle();
            tools[(int)ShapeType.RectangleR] = new ToolRectangleR();
            tools[(int)ShapeType.Circle] = new ToolCircle();
            tools[(int)ShapeType.Ellipse] = new ToolEllipse();
            tools[(int)ShapeType.Line] = new ToolLine();
            tools[(int)ShapeType.Polygon] = new ToolPolygon();
        }
        #endregion

        #region 公共方法
        public void AddCommandToHistory(ICommand command)
        {
            undoManager.AddCommandToHistory(command);
        }
        /// <summary> 撤销 </summary>
        public void Undo()
        {
            undoManager.Undo();
            Refresh();
        }
        /// <summary> 重做 </summary>
        public void Redo()
        {
            undoManager.Redo();
            Refresh();
        }
        /// <summary> 标记 </summary>
        public void SetDirty()
        {

        }
        #endregion

        #region  事件：重写
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (ShapeList != null)
            {
                ShapeList.Draw(e.Graphics);
            }
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
                tools[(int)activeTool].OnMouseDown(this, e);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left || e.Button == MouseButtons.None)
                tools[(int)activeTool].OnMouseMove(this, e);
            else
                this.Cursor = Cursors.Default;
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left)
                tools[(int)activeTool].OnMouseUp(this, e);
        }
        #endregion
    }
}
